<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="vue.js"></script>
    <script src="vue-router.js"></script>
</head>

<body>
    <!-- 
        路由组件传参、
        在组件中使用 $route 会使之与其对应路由形成高度耦合，从而使组件只能在某些特定的 URL 上使用，限制了其灵活性。
        使用 props 将组件和路由解耦：
        取代与 $route 的耦合 
    -->
    <div id="app">
        <router-link to="/user/pangchao">顶级路由</router-link>
        <router-link to="/redirect/chaoYue">重定向路由</router-link>
        <router-link to="/user/ziyang/sub1">嵌套子路由1</router-link>
        <router-link to="/user/runoob/sub2">嵌套子路由2</router-link>
        <div @click="r_c">Click Me</div>
        <router-view></router-view>
        <router-view name="users"></router-view>
        <router-view name="guest"></router-view>
    </div>
    <script>
        let defaults = {
            template: "<div> <h1> {{ $route.params.name }} </h1>  <router-view></router-view> </div>"
        }
        let users = {
            template: '<div> this is user </div>'
        }

        let guest = {
            template: "<h3> this is guest </h3>"
        }

        let children1 = {
            template: "<span> this is user children 1 components </span>"
        }

        let children2 = {
            template: "<span> this is user children 2 components </span>"
        }

        const router = new VueRouter({
            routes: [{
                path: '/user/:name',
                name: "user",
                components: {
                    default: defaults,
                    guest,
                    users
                },
                children: [{
                    path: 'sub1',
                    component: children1
                }, {
                    path: 'sub2',
                    component: children2
                }]
            }, {
                path: '/redirect/:name',
                name: 'redirect',
                redirect: to => '/user/:name'
            }]
        })

        new Vue({
            router,
            methods: {
                r_c() {
                    router.push({
                        name: 'redirect',
                        params: {
                            name: 'what fuck?'
                        }
                    })
                }
            }
        }).$mount('#app')
    </script>
</body>

</html>